<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Affiliate extends CI_Controller {

	var $per_page;	
	var $user_id;

	public function __construct()
	{
		parent::__construct();

		$logged_in = $this->session->userdata('logged_in');
		if($logged_in)
		{
		   $session_data = $this->session->userdata('logged_in');
		   $data['username'] = $session_data['username'];
		   $this->user_id = $session_data['id'];
		}
		else
		{
	     //If no session, redirect to login page
	     redirect('login', 'refresh');
   		}   	

		$this->template->set('nav', $this->uri->segment(1));
		$this->template->set('title', 'Cộng tác viên');
		$this->template->set('section', 'Xem doanh thu');	
	
		////////////// load product model, order /////////////////////////
		$this->load->model('Product_model');
		$this->load->model('Order_model');
		$this->load->model('Order_sizes_model');
		$this->load->model('Storage_product_model');
		$this->load->model('Storage_model');
		$this->load->model('Code_export_form_model');
		$this->load->model('Product_sizes_model');
		$this->load->model('Storage_product_history_model');		
		$this->load->model('User_model');
		$this->load->model('User_revenue_model');
		$this->load->model('History_revenue_model');
		/////////////////////////////////////////////////////////////////			
	}	

	public function index()
	{
		$data = array();
		$search = array();		

		/////////////////////// TÌM KIẾM /////////////////////////////////////////////////////////////////////			
		$fromDate = $this->input->post('fromDate');
		$toDate = $this->input->post('toDate');		
		$now = date('Y-m-d', strtotime('+1 day', strtotime('now')));
		$last7day = date('Y-m-d',strtotime('-7 days', strtotime('now')));

		$data['fromDate'] = $data['toDate'] = '';
		
		if(!empty($fromDate)){
			$search['from_date'] = $fromDate;
			$data['fromDate'] = $fromDate;
		}
		else
		{
			$search['from_date'] = $last7day;
			$data['fromDate'] = $last7day;
		}

		if(!empty($toDate)){
			$search['to_date'] = $toDate;
			$data['toDate'] = $toDate;
		}	
		else
		{
			$search['to_date'] = $now;
			$data['toDate'] = $now;
		}	
		///////////////////////////////////////////////////////////////////////////////////////////////////////

		//if(!empty($fromDate) && !empty($toDate))
		{
			$data['history'] = $this->History_revenue_model->getHistory($data['fromDate'], $data['toDate'], $this->user_id);

			//echo $this->db->last_query();

			$history = array();
			foreach ($data['history'] as $row) 
			{
				$date_created = date('Y-m-d', strtotime($row->date_created));
				$history[$date_created] = array(
						'revenue'	=>	$row->revenue,
						'qty'		=>	$row->qty,
					);
			}

			$showData = array();
			$nextDate = $data['fromDate'];
			$toDate = $data['toDate'];

			$data['total_revenue'] = $data['total_qty'] = '';

			while (strtotime($nextDate) <= strtotime($toDate)) 
			{
				$showData[$nextDate]['revenue'] = isset($history[$nextDate]['revenue']) ? $history[$nextDate]['revenue'] : 0;
				$showData[$nextDate]['qty'] = isset($history[$nextDate]['qty']) ? $history[$nextDate]['qty'] : 0;									

				$data['total_revenue'] += $showData[$nextDate]['revenue'];
				$data['total_qty'] += $showData[$nextDate]['qty'];

				$nextDate = date('Y-m-d', strtotime("+1 day", strtotime($nextDate)));						
			}	

			if(!empty($showData))
			{
				$data['showData'] = $showData;		
			}	
		}

		$this->template->load_main('admin/template','admin/affiliate',$data);
	}

	public function search()
	{
		$this->template->load_main('admin/template','admin/affiliate');
	}

}		